package com.firmboy.schedu.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * redis的连接操作类
 *
 * @author firmboy
 * @create 2018-03-13 下午5:22
 **/
@Repository
public class RediaDao {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private RedisTemplate redisTemplate;




    public void setKey(String key,String value){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key, value);
    }

    public String getKey(String key){
        ValueOperations<String, String> ops = template.opsForValue();
        String s = ops.get(key);
        return s;
    }

    /**
     * 根据key，判断是否获取到了可以执行的key
     * @param key
     * @return
     */
    public boolean getLock(String key){
        ValueOperations<String, String> ops = template.opsForValue();
        Boolean lock = ops.setIfAbsent(key, "lock");


        return lock;
    }

    /**
     * 根据key，释放对应的锁
     * @param key
     */
    public void releaseLock(String key){
        template.delete(key);
    }

}
